package in.muton.edushare.processing.objects;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.dao.UsersDAO;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class ObjectTypeId {
	public static CayenneList<Users> getOwner(int id, int type) {
		CayenneList<Users> uL = new CayenneList<Users>(null);
		switch (type) {
		case (Messages.RECEIVER_TYPE_UNIVERSITY):
			UnivDataHandler uD = new UnivDataHandler(getUniv(id));
			return uD.getAdmins();
		case (Messages.RECEIVER_TYPE_DEPARTMENT):
			DeptDataHandler dD = new DeptDataHandler(getDept(id));
			CayenneList<Users> userList = dD.getPrimaryAdmins();
			userList.add(dD.getUnivAdmins());
			return userList;
		case (Messages.RECEIVER_TYPE_COURSE):
			CourseDataHandler cD = new CourseDataHandler(getCourse(id));
			CayenneList<Users> userList1 = cD.getTeachers();
			DeptDataHandler dD1 = new DeptDataHandler(cD.getOwner());
			UnivDataHandler uD1 = new UnivDataHandler(cD.getOwnerUniv());
			userList1.add(dD1.getPrimaryAdmins());
			userList1.add(uD1.getAdmins());
			return userList1;
		case (Messages.RECEIVER_TYPE_USER):
			CayenneList<Users> cL = new CayenneList<Users>(null);
			cL.add(new UsersDAO(EduShareSession.get()
					.getSessionORMInterface()).getUserByPk(id));
			return cL;
		}
		return uL;
	}

	private static IModel<Universities> getUniv(int id) {
		return new CayenneModel<Universities>(new UniversitiesDAO(
				EduShareSession.get().getSessionORMInterface()).getUnivById(id));
	}

	private static IModel<Departments> getDept(int id) {
		return new CayenneModel<Departments>(new DepartmentsDAO(EduShareSession
				.get().getSessionORMInterface()).getDepartmentByPk(id));
	}

	private static IModel<CourseInstances> getCourse(int id) {
		return new CayenneModel<CourseInstances>(new CourseInstancesDAO(EduShareSession
				.get().getSessionORMInterface()).getCourseInstanceById(id));
	}

}
